package com.efast.dmom.equipment.service.repair.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.efast.cafe.framework.jdbc.JdbcDao;
import com.efast.cafe.framework.util.SpringWebUtils;
import com.efast.dmom.equipment.bean.repair.EquipRepairEquipInfoBean;
import com.efast.dmom.equipment.bean.repair.EquipRepairInfoBean;
import com.efast.dmom.equipment.bean.repair.EquipRepairOrderBean;
import com.efast.dmom.equipment.bean.repair.EquipRepairPlanBean;
import com.efast.dmom.equipment.bean.repair.EquipSpareConsumptionBean;
import com.efast.dmom.equipment.dao.repair.EquipRepairOrderDao;
import com.efast.dmom.equipment.dao.repair.EquipRepairPlanDao;
import com.efast.dmom.equipment.service.repair.IEquipRepairPlanService;

@Service
public class EquipRepairPlanServiceImpl implements IEquipRepairPlanService {

	@Autowired
	public EquipRepairPlanDao planDao = null;

	@Autowired
	EquipRepairOrderDao orderDao = null;

	@Autowired
	private JdbcDao jdbcDao;

	@Override
	public void saveRepairPlan(EquipRepairPlanBean target) {

		List<EquipRepairEquipInfoBean> equipList = target.getEquipList();

		EquipRepairEquipInfoBean equipInfoBean = null;

		List<EquipRepairEquipInfoBean> equipInfoList = new ArrayList<EquipRepairEquipInfoBean>();

		for (int i = 0; i < equipList.size(); i++) {

			EquipRepairPlanBean planBean = new EquipRepairPlanBean();

			planBean.setCreateUser(SpringWebUtils.getRequestUser());
			planBean.setLastUpdateTime(new Date());
			planBean.setLastUpdateUser(SpringWebUtils.getRequestUser());
			planBean.setCreateTime(new Date());
			planBean.setCompanyCode(SpringWebUtils.getRequestCompany());
			planBean.setPlanStatus(target.getPlanStatus());

			planBean.setEquipCode(equipList.get(i).getEquipCode());
			planBean.setEquipDepartment(equipList.get(i).getDepartment());
			planBean.setEquipName(equipList.get(i).getEquipName());
			planBean.setEquipModel(equipList.get(i).getEquipModel());
			planBean.setEquipStatus(equipList.get(i).getEquipStatus());

			planBean.setExternalUnits(target.getExternalUnits());
			planBean.setFaultDesc(target.getFaultDesc());
			planBean.setFaultLevel(target.getFaultLevel());
			planBean.setFaultType(target.getFaultType());
			planBean.setOccurTime(target.getOccurTime());
			planBean.setPlanDesc(target.getPlanDesc());
			planBean.setReminderDays(target.getReminderDays());
			planBean.setRepairGroup(target.getRepairGroup());
			planBean.setRepairLevel(target.getRepairGroup());
			planBean.setRepairPerson(target.getRepairPerson());
			planBean.setRepairType(target.getRepairType());
			planBean.setReporter(target.getReporter());
			planBean.setReportTime(target.getReportTime());
			planBean.setUrgencyLevel(target.getUrgencyLevel());
			planBean.setEquipLocation(equipList.get(i).getLocation());
			planBean.setPlanEndTime(target.getPlanStartTime());
			planBean.setPlanStartTime(target.getPlanStartTime());
			planBean.setUnit(target.getUnit());
			planBean.setPlanNumber(jdbcDao.executeProcGetCode("JH", SpringWebUtils.getRequestCompany()));
			planBean.setPlanName(target.getPlanName());
			planBean.setEquipCategory(equipList.get(i).getEquipCategory());

			planDao.saveRepairPlanBaseInfo(planBean);

			equipInfoBean = new EquipRepairEquipInfoBean();
			equipInfoBean.setDepartment(equipList.get(i).getDepartment());
			equipInfoBean.setEquipCategory(equipList.get(i).getEquipCategory());
			equipInfoBean.setEquipCode(equipList.get(i).getEquipCode());
			equipInfoBean.setEquipModel(equipList.get(i).getEquipModel());
			equipInfoBean.setEquipName(equipList.get(i).getEquipName());
			equipInfoBean.setLocation(equipList.get(i).getLocation());
			equipInfoBean.setRepairPlanId(planBean.getId());
			equipInfoBean.setEquipStatus(equipList.get(i).getEquipStatus());

			equipInfoBean.setCreateUser(SpringWebUtils.getRequestUser());
			equipInfoBean.setLastUpdateTime(new Date());
			equipInfoBean.setLastUpdateUser(SpringWebUtils.getRequestUser());
			equipInfoBean.setCreateTime(new Date());
			equipInfoBean.setCompanyCode(SpringWebUtils.getRequestCompany());

			equipInfoList.add(equipInfoBean);
		}

		planDao.saveRepairPlanEquipInfo(equipInfoList);
	}

	public void createRepairOrder(EquipRepairPlanBean planBean) {

		if (planBean != null) {

			EquipRepairOrderBean orderBean = new EquipRepairOrderBean();

			orderBean.setCreateUser(SpringWebUtils.getRequestUser());
			orderBean.setLastUpdateTime(new Date());
			orderBean.setLastUpdateUser(SpringWebUtils.getRequestUser());
			orderBean.setCreateTime(new Date());
			orderBean.setCompanyCode(SpringWebUtils.getRequestCompany());

			orderBean.setOrderNumber(jdbcDao.executeProcGetCode("WX", SpringWebUtils.getRequestCompany()));
			orderBean.setStatus("todo");
			orderBean.setRepairGroup(planBean.getRepairGroup());
			orderBean.setRepairer(planBean.getRepairPerson());
			orderBean.setEquipName(planBean.getEquipList() != null && planBean.getEquipList().size() > 0
					? planBean.getEquipList().get(0).getEquipName() : null);
			orderBean.setEquipCode(planBean.getEquipList() != null && planBean.getEquipList().size() > 0
					? planBean.getEquipList().get(0).getEquipCode() : null);
			orderBean.setEquipModel(planBean.getEquipList() != null && planBean.getEquipList().size() > 0
					? planBean.getEquipList().get(0).getEquipModel() : null);
			orderBean.setEquipCategory(planBean.getEquipList() != null && planBean.getEquipList().size() > 0
					? planBean.getEquipList().get(0).getEquipCategory() : null);
			orderBean.setFaultDesc(planBean.getFaultDesc());
			orderBean.setDepartment(planBean.getEquipDepartment());
			orderBean.setReportor(planBean.getReporter());
			orderBean.setReportTime(planBean.getReportTime());
			orderBean.setOccurTime(planBean.getOccurTime());
			orderBean.setRepairLevel(planBean.getRepairLevel());
			orderBean.setRepairType(planBean.getRepairType());
			orderBean.setFaultType(planBean.getFaultType());
			orderBean.setFaultDesc(planBean.getFaultDesc());
			orderBean.setEmergency(planBean.getUrgencyLevel());
			orderBean.setPlanId(planBean.getId());
			orderBean.setFaultLevel(planBean.getFaultLevel());
			orderBean.setEquipLocation(planBean.getEquipLocation());

			orderDao.createRepairOrder(orderBean);

			planDao.updateRepairPlanStatus(planBean.getId(), "started");
		}
	}

	@Override
	public void updateRepairPlanStatus(Integer planId, String status) {

		planDao.updateRepairPlanStatus(planId, status);
	}

	public void updateRepairPlan(EquipRepairPlanBean planBean) {

		if (planBean.getEquipList() != null && planBean.getEquipList().size() > 0) {
			planDao.deleteRepairPlanEquipInfo(planBean.getId());
			planDao.saveRepairPlanEquipInfo(planBean.getEquipList());
		}
		planDao.updateRepairPlan(planBean);
	}

	@Override
	public EquipRepairPlanBean getRepairPlanBaseInfoById(Integer planId) {

		return planDao.getRepairPlanBaseInfoById(planId);
	}

	@Override
	public void deleteRepairPlan(Integer planId) {

		planDao.deleteRepairPlan(planId);
	}
	
	@Override
	public List<EquipRepairInfoBean> getRepairInfoByPlanId(Integer id) {
		return orderDao.getRepairInfoByPlanId(id);
	}


	@Override
	public List<EquipSpareConsumptionBean> getSpareInfoByPlanId(Integer id) {
		return orderDao.getSpareInfoByPlanId(id);
	}

}
